# coding = 'utf-8'

import xml.dom.minidom as xmldoc
import os,uuid,json,time
import xmltodict


class xml(object):
    """docstring for xml"""
    def __init__(self):
        super(xml ).__init__()
    def readXML(xml):
        try:
            tree=None
            if os.path.isfile(xml):
                with open(xml,encoding='utf-8') as file:
                    string=file.read()
                    xmlDict = xmltodict.parse(string)
                    return json.loads(json.dumps(xmlDict))
            else:
                xmlDict = xmltodict.parse(xml)
                return json.loads(json.dumps(xmlDict))
        except Exception as e:
            pass

    def writeXML(mData,subData,msgid,SRCSEC):
        #在内存中创建一个空的文档
        doc=''
        doc =xmldoc.Document()
        YGCT = doc.createElement('YGCT')
        doc.appendChild(YGCT)
        #父节点
        HEAD = doc.createElement('HEAD')
        YGCT.appendChild(HEAD)
        TransDate   =doc.createElement("TransDate")
        TransDate.appendChild(doc.createTextNode(str(time.strftime("%Y%m%d%H%M%S", time.localtime()))))
        Reserve     =doc.createElement("Reserve")
        Reserve.appendChild(doc.createTextNode(''))
        MsgNo       =doc.createElement("MsgNo")
        MsgNo.appendChild(doc.createTextNode(msgid))
        guid=str(uuid.uuid1())
        guid=guid[4:]
        MsgId       =doc.createElement("MsgId")
        MsgId.appendChild(doc.createTextNode(guid))
        SRC         =doc.createElement("SRC")
        SRC.appendChild(doc.createTextNode(SRCSEC))
        MsgRef      =doc.createElement("MsgRef")
        MsgRef.appendChild(doc.createTextNode(''))
        DESC        =doc.createElement("DES")
        DESC.appendChild(doc.createTextNode(SRCSEC))
        VERSION     =doc.createElement("VERSION")
        VERSION.appendChild(doc.createTextNode('1.0'))

        HEAD.appendChild(TransDate)
        HEAD.appendChild(Reserve)
        HEAD.appendChild(MsgNo)
        HEAD.appendChild(MsgId)
        HEAD.appendChild(SRC)
        HEAD.appendChild(MsgRef)
        HEAD.appendChild(DESC)
        HEAD.appendChild(VERSION)
        #子节点
        MSG = doc.createElement('MSG')
        YGCT.appendChild(MSG)
        #子节点
        MAIN= doc.createElement('CzpBack%s'%msgid)
        MSG.appendChild(MAIN)
        #主表数据
        for key in mData.keys():
            mkey=doc.createElement(key)
            mkey.appendChild(doc.createTextNode(str(mData[key])))
            MAIN.appendChild(mkey)
        #子表数据
        for row in subData :
            #创建重孙节点
            List = doc.createElement('CzpBack%sSubList'%msgid)
            for k in row.keys():
                key=doc.createElement(k)
                key.appendChild(doc.createTextNode(str(row[k])))
                List.appendChild(key)
            MAIN.appendChild(List)
        return doc.toxml(encoding="UTF-8").decode("UTF-8")




if __name__ == '__main__':
    l=r'D:\ICS\宝二\MC\BillFile\20181113132930861_00201.efl'
    s=r'D:\ICS\宝二\存样柜\data\reveicer\20181028110328677_00201.efl'
    # data=xml.readXML(l)

    # print(data)
    print(dir(xmldoc))






